//  [10/14/2013 OMEGA] created

#include <QtWidgets/QWidget>
#include <QtWidgets/QBoxLayout>
#include "DuelGUIECommon.h"
#include "DuelCore.h"
#include "DuelRenderSystem.h"
#include "DuelFrameBuffer.h"
#include "DuelGUIEWorkbench.h"
#include "DuelGUIEEditPanel.h"

using namespace Duel;

DGUIEGraphicsView::DGUIEGraphicsView( const QString& name, DGUIEGraphicsRenderHandler* rendHandler, DGUIEGraphicsViewEventHandler* eventHandler, QWidget* parent ) :
	QWidget(parent),
	mRenderHandler(rendHandler),
	mEventHandler(eventHandler),
	mName(name),
	mbFocused(false)
{
	QHBoxLayout* layout = new QHBoxLayout(this);
	layout->setMargin(0);

	setAttribute(Qt::WA_PaintOnScreen);
	setAttribute(Qt::WA_NoSystemBackground);

	//setMinimumSize(QSize(50, 50));
	WId handle = winId();
	RenderWindowSetting wndSetting;

	wndSetting.left = 0;
	wndSetting.top = 0;
	wndSetting.width = 50;
	wndSetting.height = 50;
	wndSetting.samplerCount = 1;
	wndSetting.samplerQuality = 1;
	wndSetting.verticalSync = false;
	wndSetting.useGamma = false;
	wndSetting.surfaceFormat = Duel::PF_A8R8G8B8;
	mRenderWindow = DRenderResourceManager::getSingleton().createRenderWindow(
		DString(mName.toLocal8Bit()), wndSetting, handle);

}

DGUIEGraphicsView::~DGUIEGraphicsView()
{
	DRenderResourceManager::getSingleton().destroyRenderWindow(mRenderWindow);
	//
}

void DGUIEGraphicsView::paintEvent( QPaintEvent *e )
{
	DGUIEWorkbench::getSingleton().requestUpdate(this);
}
void DGUIEGraphicsView::resizeEvent( QResizeEvent *p_event )
{
	QSize newSize = size();
	if(p_event)
	{
		newSize = p_event->size();
		// if( width()==newSize.width() && height()==newSize.height() ) return;
		QWidget::resizeEvent( p_event );
	}
	mRenderWindow->resize(newSize.width(), newSize.height());
	DGUIEWorkbench::getSingleton().requestUpdate(this);
}

void DGUIEGraphicsView::mousePressEvent( QMouseEvent* e)
{
	if (mEventHandler)
	{
		mEventHandler->handleMousePressEvent(e);
	}
	else
	{
		QWidget::mousePressEvent(e);
	}
	std::cout << mName.toStdString() << "MousePressed" << std::endl;
}

void DGUIEGraphicsView::mouseReleaseEvent( QMouseEvent* e)
{
	if (mEventHandler)
	{
		mEventHandler->handleMouseReleaseEvent(e);
	}
	else
	{
		QWidget::mouseReleaseEvent(e);
	}
	std::cout << mName.toStdString() << "MouseReleased" << std::endl;
}

void DGUIEGraphicsView::mouseMoveEvent( QMouseEvent* e)
{
	if (mEventHandler)
	{
		mEventHandler->handleMouseMoveEvent(e);
	}
	else
	{
		QWidget::mouseMoveEvent(e);
	}
	std::cout << mName.toStdString() << "MouseMoved" << std::endl;
}

void DGUIEGraphicsView::wheelEvent( QWheelEvent* e)
{
	if (mEventHandler)
	{
		mEventHandler->handleWheelEvent(e);
	}
	else
	{
		QWidget::wheelEvent(e);
	}
	std::cout << mName.toStdString() << "WheelEvent" << std::endl;
}

void DGUIEGraphicsView::keyPressEvent( QKeyEvent* e)
{
	if (mEventHandler)
	{
		mEventHandler->handleKeyPressEvent(e);
	}
	else
	{
		QWidget::keyPressEvent(e);
	}
	std::cout << mName.toStdString() << "KeyPressed" << std::endl;
}

void DGUIEGraphicsView::keyReleaseEvent( QKeyEvent* e)
{
	if (mEventHandler)
	{
		mEventHandler->handleKeyReleaseEvent(e);
	}
	else
	{
		QWidget::keyReleaseEvent(e);
	}
	std::cout << mName.toStdString() << "KeyReleased" << std::endl;
}